<!--
 * @Author: your name
 * @Date: 2021-07-03 08:12:41
 * @LastEditTime: 2021-07-03 14:50:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vuecli-demo\src\App.vue
-->
<template>
  <div>
    <section class="todoapp">
      <TodoHeader @add-todo="Addfn"/>
      <TodoMain 
        :arr="showList"
        @del-todo="DelFn"/>
      <TodoFooter 
        v-if="list.length > 0" 
        :arr='list'
        @clear-todo='btnClearFn'
        @switch-todo='fSwitchFn'/>
    </section>
  </div>
</template>

<script>
import "/src/assets/css/base.css";
import "/src/assets/css/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  
  data () {
    return {
      fatherType:'all',
      list: JSON.parse(window.localStorage.getItem('tpdp')) || [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
    }
  },
  methods: {
    Addfn(name){
      const obj = {
        if: Date.now(),
        name,
        isDone:false
      }
      this.list.push(obj)
    },
    DelFn(i){
      if(window.confirm('是否删除？')){
        this.list.splice(i,1)
      }
    },
    fSwitchFn(switchfn){
      console.log(switchfn);
      this.fatherType = switchfn
    },
    btnClearFn(){
      this.list = this.list.filter(item => item.isDone === false)
    }
  },
  computed: {
    showList(){
      if(this.fatherType === 'all'){
        return this.list
      }else if(this.fatherType === 'no'){
        return this.list.filter(item => item.isDone === false)
      }else {
        return this.list.filter(item => item.isDone === true)
      }
    }
  },
  watch: {
    list:{
      deep:true,
      handler(newVal){
        window.localStorage.setItem('todo',JSON.stringify(newVal))
      }
    }
  }
};
</script>

<style>
</style>